<?php
/**
 * Created by PhpStorm.
 * User: fire
 * Date: 2018/12/28
 * Time: 10:35
 */
namespace App;

use Illuminate\Support\Facades\Redis;

trait RecordsVisits {

    public function resetVisits()
    {
        Redis::del($this->visitsCacheKey());

        return $this;
    }

    public function recordVisit()
    {
        Redis::incr($this->visitsCacheKey());
        return $this;
    }

    public function visits()
    {
        return Redis::get($this->visitsCacheKey()) ?: 0;

    }

    public function visitsCacheKey()
    {
        return "threads.{$this->id}.visits";
    }

}